package com.tencent.iov.cowin.befsale.clue.service.clue.impl;

import com.github.pagehelper.Page;
import com.google.common.collect.Lists;
import com.tencent.iov.cowin.befsale.clue.controller.clue.vo.response.ClueToMqVehResponse;
import com.tencent.iov.cowin.befsale.clue.domain.clue.po.ClueIntentionVehicle;
import com.tencent.iov.cowin.befsale.clue.repository.clue.ClueIntentionVehicleManager;
import com.tencent.iov.cowin.befsale.clue.service.clue.ClueIntentionVehicleService;
import com.software.boot.common.exception.BusinessException;
import com.software.boot.common.exception.NEError;
import com.software.boot.common.model.dao.PageRequest;
import com.software.boot.common.model.dao.PageResponse;
import com.software.boot.common.util.BeanUtil;
import com.software.boot.common.util.EmptyUtils;
import com.software.boot.common.util.ValidateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Describe: 意向车车型_Service实现层
 * @author: xiamu
 * @created: 2022-01-06 14:53:45
 * @version: 1.0
 */
@Service
public class ClueIntentionVehicleServiceImpl implements ClueIntentionVehicleService {

    @Autowired
    private ClueIntentionVehicleManager clueIntentionVehicleManager;

    /**
     * 通过ID获取详细信息
     *
     * @param: request
     * @return: ClueIntentionVehicle对象
     */
    @Override
    public ClueIntentionVehicle getDetailById(Long id) {
        /** 1 验证请求参数 */
        ValidateUtils.checkObjNull(id, "请求参数不能为空");
        ValidateUtils.checkIntNotNullAndGTZero(id.intValue(), "ID不能为空或小于0");
        /** 2 处理业务 */
        ClueIntentionVehicle clueIntentionVehicle = clueIntentionVehicleManager.getDetailById(id);
        if (EmptyUtils.isNotEmpty(clueIntentionVehicle)) {
            return BeanUtil.copy(clueIntentionVehicle, ClueIntentionVehicle.class);
        }
        /** 3 返回结果 */
        return null;
    }

    @Override
   // @Transactional(rollbackFor = Exception.class)
    public Long insert(ClueIntentionVehicle request) {
        //验证请求参数
        ValidateUtils.checkLongGTZero(request.getClueId(), "线索ID不可以为null");
        //删除原先保存数据
        clueIntentionVehicleManager.deleteByClueIds(Lists.newArrayList(request.getClueId()));
        //处理业务
        int count = clueIntentionVehicleManager.insert(request);
        if (count <= 0) {
            throw new BusinessException(NEError.BUSINESS_ERROR, "保存车系车系信息失败");
        }
        //封装返回值
        return request.getClueId();
    }

    @Override
//    @Transactional(rollbackFor = Exception.class)
    public int insertBatch(List<ClueIntentionVehicle> requests) {
        //验证请求参数
        ValidateUtils.checkObjNull(requests, "参数为null");
        //删除原先保存数据
        List<Long> cluIds = requests.stream().map(ClueIntentionVehicle::getClueId).collect(Collectors.toList());
        if (EmptyUtils.isEmpty(cluIds)) {
            throw new BusinessException(NEError.INVALID_PARAMETER, "线索ID不可以为null");
        }
        clueIntentionVehicleManager.deleteByClueIds(cluIds);
        //封装返回值
        return clueIntentionVehicleManager.insertBatch(requests);
    }


    /**
     * 单条ClueIntentionVehicle更新
     *
     * @param request
     * @return 更新的条数
     */
    @Override
    public int update(ClueIntentionVehicle request) {

        /** 1 验证请求参数 */
        /** 2 处理业务 */
        int count = clueIntentionVehicleManager.update(request);
        /** 3 返回对象 */
        return count;
    }

    /**
     * 通过ID删除数据
     *
     * @param request
     * @return 删除的条数
     */
    @Override
    public int delete(ClueIntentionVehicle request) {
        Long id = request.getId();
        /** 1 验证请求参数 */
        ValidateUtils.checkObjNull(id, "请求参数不能为空");
        ValidateUtils.checkIntNotNullAndGTZero(id.intValue(), "ID不能为空或小于0");
        /** 2 处理业务 */
        ClueIntentionVehicle clueIntentionVehicle = new ClueIntentionVehicle();
        clueIntentionVehicle.setId(id);
//		clueIntentionVehicle.setDelete(DeleteEnum.DETELED.getCode());
        int count = clueIntentionVehicleManager.update(clueIntentionVehicle);
        /** 3 返回对象 */
        return count;
    }

    /**
     * 根据条件查询对象对象list列表
     *
     * @param request
     * @return 查询结果
     */
    @Override
    public List<ClueIntentionVehicle> getListByParam(ClueIntentionVehicle request) {
        /** 1 验证请求参数 */
        //可以设置排序
        /** 2 处理业务 */
        List<ClueIntentionVehicle> queryList = clueIntentionVehicleManager.getListByParam(request);
        /** 3 返回结果 */
        if (CollectionUtils.isNotEmpty(queryList)) {
            return BeanUtil.copyByList(queryList, ClueIntentionVehicle.class);
        }
        return new ArrayList<>();
    }

    /**
     * 根据条件查询对象list记录
     *
     * @param request
     * @return 分页对象
     */
    @Override
    public PageResponse<ClueIntentionVehicle> getPageByParam(PageRequest<ClueIntentionVehicle> request) {
        /** 1 验证请求参数 */
        ValidateUtils.checkObjNull(request, "请求参数不能为空");
        //可以设置排序 query.orderByCreatedTime(true);
        /** 2 处理业务 */
        RowBounds rowBounds = new RowBounds(request.getPageNum(), request.getPageSize());
        Page<ClueIntentionVehicle> queryPageList = clueIntentionVehicleManager.getPageByParam(request.getParamData(), rowBounds);
        PageResponse<ClueIntentionVehicle> pageResponse = new PageResponse<>();
        pageResponse.converPageResponse(pageResponse, queryPageList, request);
        pageResponse.setDataList(BeanUtil.copyByList(queryPageList.getResult(), ClueIntentionVehicle.class));
        /** 3 返回结果 */
        return pageResponse;
    }

    /**
     * 根据条件查询单个对象
     *
     * @param request
     * @return
     */
    @Override
    public ClueIntentionVehicle getOneByParam(ClueIntentionVehicle request) {
        return clueIntentionVehicleManager.getOneByParam(request);
    }

    @Override
    public List<ClueToMqVehResponse> getListByClueId(Long clueId) {
        return clueIntentionVehicleManager.getListByClueId(clueId);
    }
}
